using System;
using System.Collections.Generic;
using MvpFramework.DesignByContract;

namespace MvpFramework
{
    /// <summary>
    /// Interface for Validation Managers
    /// </summary>
    public interface IValidationManager
    {
        /// <summary>
        /// Should return All Validation Labels for a Widget
        /// </summary>
        /// <returns></returns>
        IList<IValidationMessage> GetAllValidationLabels();

        /// <summary>
        /// Should return the Validation Label for a specification
        /// </summary>
        /// <param name="specification"></param>
        /// <returns></returns>
        IValidationMessage GetValidationLabelFor(object specification);
    }
    /// <summary>
    /// A class that does the majority of work for IValidation Manager
    /// </summary>
    public abstract class ValidationManager : IValidationManager
    {
        /// <summary>
        /// The Function that will find the <see cref="IValidationMessage"/> from the list of All Validations
        /// </summary>
        public abstract Func<object, IValidationMessage> FindLabelFunction { get; }

        #region IValidationManager Members

        /// <summary>
        /// Should return All Validation Labels for a Widget
        /// </summary>
        /// <returns></returns>
        public abstract IList<IValidationMessage> GetAllValidationLabels();

        /// <summary>
        /// Returns the <see cref="IValidationMessage"/> for the Specification sent
        /// </summary>
        /// <param name="specification"></param>
        /// <returns></returns>
        public IValidationMessage GetValidationLabelFor(object specification)
        {
            CheckFindLabelContract();
            return FindLabelFunction.Invoke(specification);
        }

        #endregion

        private void CheckFindLabelContract()
        {
            Check.Require(FindLabelFunction != null, "Property FindLabelFunction can not be null");
        }
    }
}